<?php

/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-01-20
 * 版    本：1.0.0
 * 功能说明：用户控制器。
 *
 * */

namespace Qwadmin\Controller;

class ShopSiteApiController extends ComController {

    public function index() {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $order = isset($_GET['order']) ? $_GET['order'] : 'DESC';
        $order = " id desc ";
        $pagesize = 20; #每页数量
        $offset = $pagesize * ($p - 1); //计算记录偏移量

        $shop_api = M('shop_api');
        $count = $shop_api->field("id")
                ->order($order)
                ->count();

        $list = $shop_api->field(" * ")
                ->order($order)
                ->limit($offset . ',' . $pagesize)
                ->select();

        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        //var_dump($list);
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }

    public function del() {
        $apiid = isset($_GET['apiid']) ? intval($_GET['apiid']) : '0';
        $where = ' id=' . $apiid;
        $shop_api = M('shop_api');
        $shop_api_vals = $shop_api->field("*")->where($where)->select();
        $this->success("成功");
    }

    public function runapi() {
        echo "run start ...ok<br>";



        $apiid = isset($_GET['apiid']) ? intval($_GET['apiid']) : '0';
        $where = ' id=' . $apiid;
        $shop_api = M('shop_api');
        $shop_api_vals = $shop_api->field("*")->where($where)->select();
        $opt = $shop_api_vals[0];
        $data = json_decode($opt['in_data']);

        echo "create execute_obj...<br>";
        $obj = new $opt['execute_obj']($apiid);
        $obj->url = $opt['url'];

        echo "obj: " . $opt['execute_obj'] . "create ok  <br>";
        $result = $obj->$opt['method']($opt['in_data']);
        echo "mothed: " . $opt['execute_obj'] . "run ok  <br>";
        print($result);
        echo "run over<br>";
        echo "<a href='/'>回首页</a><br>";
    }

    public function edit() {
        $id=I("get.id");
        $datas=M("shop_api")->find($id);
        $this->assign("data", $datas);
//        echo "<pre>"; 
//        var_dump($datas);die;
        $this->display('form');
    }

    public function update($ajax = '') {
        var_dump($_POST);die;
        $data=I("post");
        $id=$data['id'];
        unset($data['id']);
        $model=M("shop_api");
        if($id){
            $model->where(array('id'=>$id))->save($data);
        }else{
            $model->add($data);
        }
        $this->success("成功");
    }

    public function add() {

        $this->display('form');
    }

}
